// components/selectRegion/comp.js
var {request} = require('../../utils/api.js')
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    show:Boolean,
    hide:Boolean
  },
  /**
   * 组件的初始数据
   */
  data: {
    type:1,
    province:"",
    p_id:'',
    pid: '',
    city:"",
    c_id:'',
    cid: '',
    area:"",
    aid: '',
    a_id:'',
    selectList:[],
    provinceList:[],
    cityList:[],
    areaList:[],
    latitude:'',
    longitude:''
  },
  created:function(){
    this.getLocation()
    this.selectProvince()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    alert(content, title) {

      var _this = this
  
      return new Promise((resove, reject) => {
  
        wx.showModal({
          title: title || '',
          content: content,
          showCancel: false,
          confirmColor: _this.color,
          cancelColor: '#999',
          success(r) {
            if (r.confirm) {
  
              resove(r)
  
            }
          }
        })
  
      })
  
    },
    // 获取定位当前位置的经纬度
    getLocation: function () {
      let that = this;
      wx.getLocation({
        type: 'wgs84',
        success: function (res) {
          that.setData({
            latitude:res.latitude,
            longitude:res.longitude
          })
        },
        fail: function (res) {
          console.log('fail' + JSON.stringify(res))
        }
      })
    },
    selectCurrent(){
      if(!this.data.latitude){
        //需要开启定位权限
        this.checkLocation()
        return;
      }
      let params = {
        latitude:this.data.latitude,
        longitude:this.data.longitude
      }
      request('get_china_addr','post',params).then(res => {
        if (res.code == 200) {
          this.setData({
            province:res.data.province,
            city:res.data.city,
            area:res.data.district,
            p_id:res.data.province_id,
            c_id:res.data.city_id,
            a_id:res.data.area_id
          })
          if(!this.data.hide){
            this.selectArea()
          }
        }
      })
    },
    //校验位置权限是否打开
    checkLocation() {
      let _that = this;
      //选择位置，需要用户授权
      wx.getSetting({
        success(res) {
          if (!res.authSetting['scope.userLocation']) {
            wx.authorize({
              scope: 'scope.userLocation',
              success() {
                wx.showToast({ //这里提示失败原因
                  title: '授权成功！',
                  duration: 1500
                })
                _that.getLocation()
              },
              fail() {
                _that.showSettingToast('需要授权位置信息');
              }
            })
          } else {
            _that.getLocation()
          }
        }
      })
    },
    // 打开权限设置页提示框
    showSettingToast: function (e) {
      let _that = this;
      wx.showModal({
        title: '提示！',
        confirmText: '去设置',
        showCancel: true,
        content: e,
        success: function (res) {
          if (res.confirm) {
            wx.openSetting({
              success: (res) => {
                if (res.authSetting["scope.userLocation"]) {
                  _that.getLocation()
                }
              },
              fail: (res) => {
                api.tip("授权失败")
              }
            })
          }
        },
        fail: function (res) {

        }
      })
    },
    onClose() {
      let value = []
      this.data.province ? value.push(this.data.province) : ""
      this.data.city ? value.push(this.data.city) : ""
      this.data.area ? value.push(this.data.area) : ""
      let obj = {
        pid:this.data.pid,
        cid:this.data.cid,
        aid:this.data.aid,
        p_id:this.data.p_id,
        c_id:this.data.c_id,
        a_id:this.data.a_id
      }
      if(this.data.hide){
        obj.province = this.data.province
        obj.city = this.data.city
        obj.area = this.data.area
      }else{
        obj.value = value
      }
      this.triggerEvent("close",obj)
    },
    select(e){
      let code = e.currentTarget.dataset.item.code
      let id = e.currentTarget.dataset.item.id
      let name = e.currentTarget.dataset.item.name
      if(this.data.type==1){
        this.setData({
          p_id:code,
          pid:id,
          type:2,
          province:name,
          city:"",
          area:""
        })
        this.selectCity()
      }else if(this.data.type == 2){
        let type = this.data.hide ? 2 :3
        this.setData({
          c_id:code,
          cid:id,
          type,
          city:name,
          area:""
        })
        if(!this.data.hide){
          this.selectArea()
        }else{
          this.onClose()
        }
      }else{
        this.setData({
          area:name,
          aid:id,
          a_id:code
        })
        this.onClose()
      }
    },
    selectProvince(){
      request('province', 'POST').then(res => {
        if (res.data.code == 200) {
          this.setData({
            type:1,
            provinceList:res.data.data,
            selectList:res.data.data,
            c_id: ''
          })
        }
      })
    },
    choiceCity(){
      if(!this.data.p_id){
        this.alert("请先选择省份！")
        return
      }
      this.selectCity()
    },
    choiceArea(){
      if(!this.data.c_id){
        this.alert("请先选择城市！")
        return
      }
      this.selectArea()
    },
    selectCity(){
      let params={
        pid:this.data.p_id
      }
      request('city','post',params).then(res => {
        if (res.data.code == 200) {
          this.setData({
            type:2,
            cityList:res.data.data,
            selectList:res.data.data
          })
        }
      })
    },
    selectArea(){
      let params={
        cid:this.data.c_id
      }
      request('area','post',params).then(res => {
        if (res.data.code == 200) {
          this.setData({
            type:3,
            areaList:res.data.data,
            selectList:res.data.data
          })
        }
      })
    },
  }
})